#ifndef __SERV_MGMT_COMMAND_included
#define __SERV_MGMT_COMMAND_included

#include <string>
#include <vector>

using namespace std;

namespace Server {
namespace Command {

class CCommandParam {

	string mRaw;

public :

	CCommandParam(string& s) : mRaw(s) { }

	int GetInt();

	string& GetString() {
		return mRaw;
	}
};

class CCommandAction {

public :
	virtual void DoAction(vector<CCommandParam>& params, string& output) = 0;
};

class CCommandParser {

	string mData;
	int mPos;
	CCommandAction* mAction;
	vector<CCommandParam> mParams;

public :

	CCommandParser(string& cmd) : 
	   mData(cmd), mPos(0), mAction(NULL) 
	{ }

	CCommandParser(string& cmd, CCommandAction* action) : 
	   mData(cmd), mPos(0), mAction(action) 
	{ }

	bool NextToken(string& token);
	void SetAction(CCommandAction* action) { mAction = action; }
	CCommandAction* GetAction() { return mAction; }
	vector<CCommandParam>& Params() { return mParams; }
};

class CCommandTreeNode {

	string mKey;
	vector<CCommandTreeNode> mChilds;
	CCommandAction* mAction;
	bool mHasParam;

public :

	CCommandTreeNode() : mHasParam(false), mAction(NULL) { }
	CCommandTreeNode(string& key, CCommandParser& parser);

	void AddChild(string& cmd, CCommandAction* action);
	CCommandTreeNode* FindChild(string& key);
	string& Key() { return mKey; }
	bool& HasParam() { return mHasParam; }
	void Parse(CCommandParser& parser);

	// only for root
	void AddChild(string& key, CCommandParser& parser);
	void ParseAndExec(string& cmd, string& output);
	static CCommandTreeNode mCommandRoot;
};

} // Common
} // Command
#endif // __SERV_MGMT_COMMAND_included